package controller;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;

import common.StringUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import service.UserService;

@Controller
@RequestMapping({ "/user" })
@SuppressWarnings({ "rawtypes", "unchecked" })
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping({ "/list" })
	public String login(
			Model model,
			HttpServletRequest request,
			@RequestParam(value = "pageNow", defaultValue = "1") String pageNow,
			@RequestParam(value = "pageRow", defaultValue = "10") String pageRow,
			@RequestParam(value = "name", required = false) String name) {
		User u = (User) request.getSession().getAttribute("user");

		if (u == null) {
			return "/login/login";
		}

		int page = 1;
		int limit = 2;
		if ((pageNow != null) && (!"".equals(pageNow))) {
			page = Integer.parseInt(pageNow);
		}

		if ((pageRow != null) && (!"".equals(pageRow))) {
			limit = Integer.parseInt(pageRow);
		}

		PageBounds pageBounds = new PageBounds(page, limit);

		Map mapValue = new HashMap();
		mapValue.put("name", name);
		Map map = this.userService.selectAll(pageBounds, mapValue);
		List list = (List) map.get("list");
		Paginator paginator = (Paginator) map.get("paginator");

		model.addAttribute("list", list);
		model.addAttribute("paginator", paginator);

		model.addAttribute("name", name);
		model.addAttribute("user", request.getSession().getAttribute("user"));

		return "user/list";
	}

	@RequestMapping({ "/add" })
	public String add(Model model, HttpServletRequest request,
			@RequestParam(value = "userId", required = false) Long userId) {
		User u = (User) request.getSession().getAttribute("user");

		if (u == null) {
			return "/login/login";
		}
		User user = new User();
		if (!StringUtil.isEmpty(userId)) {
			user.setUserId(userId);
			user = this.userService.fingUserByUserId(user);
		} else {
			user = null;
		}
		model.addAttribute("u", user);
		return "user/add";
	}

	@RequestMapping({ "/save" })
	public String save(
			Model model,
			HttpServletRequest request,
			@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "phone", required = false) String phone,
			@RequestParam(value = "account", required = false) String account,
			@RequestParam(value = "password", required = false) String password,
			@RequestParam(value = "cardUserName", required = false) String cardUserName,
			@RequestParam(value = "cardName", required = false) String cardName,
			@RequestParam(value = "cardNo", required = false) String cardNo) {
		User u = (User) request.getSession().getAttribute("user");

		if (u == null) {
			return "/login/login";
		}

		User user = new User();
		user.setUserName(userName);
		user.setPhone(phone);
		user.setAccount(account);
		user.setPassword(password);
		user.setCardUserName(cardUserName);
		user.setCardName(cardName);
		user.setCardNo(cardNo);
		user.setCreateUser(u.getUserId());
		user.setCreateDate(new Date());
		user.setUpdateUser(u.getUserId());
		user.setUpdateDate(new Date());
		user.setUserType(2l);
		user.setState(1l);
		user.setScore(0);
		user.setIncome(0l);

		if (StringUtil.isEmpty(userId)) {
			this.userService.insert(user);
		} else {
			user.setUserId(userId);
			this.userService.updateByPrimaryKey(user);
		}

		return "redirect:/user/list";
	}

	@RequestMapping({ "/updateStatus" })
	public String updateStatus(Model model,
			@RequestParam(value = "pageNow", required = false) String pageNow,
			@RequestParam(value = "pageRow", required = false) String pageRow,
			@RequestParam(value = "userId", required = false) String userId,
			@RequestParam(value = "status", required = false) String status) {
		Map map = new HashMap();
		map.put("userId", userId);
		map.put("status", status);

		this.userService.updateStatus(map);

		return "redirect:/user/list?pageNow=" + pageNow + "&pageRow=" + pageRow;
	}

	@RequestMapping({ "loginOut" })
	public String loginOut(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "login/login";
	}
}